package cjm.task5.controller;

import cjm.task5.pojo.Record;
import cjm.task5.pojo.User;
import cjm.task5.pojo.UserMessage;
import cjm.task5.service.RecordService;
import cjm.task5.service.UserService;

import org.apache.poi.hssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
public class ExcelController {
    @Autowired
    UserService userService;
    @Autowired
    private RecordService recordService;

    //管理员表导出
    @RequestMapping(value = "AdExcel")
    public void AdExport(HttpServletResponse response) throws IOException {
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("信息表");
        List<User> users = userService.findAllAdUser();
        // 设置要导出的文件的名字
        String fileName = "管理员表"  + new Date() + ".xls";
        // 新增数据行，并且设置单元格数据
        int rowNum = 1;
        // headers表示excel表中第一行的表头 在excel表中添加表头
        String[] headers = { "id", "账号", "姓名","地址", "是否管理员","登录权限"};
        HSSFRow row = sheet.createRow(0);
        for(int i=0;i<headers.length;i++){
            HSSFCell cell = row.createCell(i);
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            cell.setCellValue(text);
        }
        //在表中存放查询到的数据放入对应的列
        for (User item : users) {
            HSSFRow row1 = sheet.createRow(rowNum);
            row1.createCell(0).setCellValue(item.getId());
            row1.createCell(1).setCellValue(item.getUserid());
            row1.createCell(2).setCellValue(item.getUsername());
            row1.createCell(3).setCellValue(item.getAddress());
            if(item.getAdmin()==1){
                row1.createCell(4).setCellValue("是");
            }
            else {
                row1.createCell(4).setCellValue("否");
            }
            if(item.getIslock()==0){
                row1.createCell(5).setCellValue("有");
            }
            else {
                row1.createCell(5).setCellValue("无");
            }
            rowNum++;
        }
        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        response.flushBuffer();
        workbook.write(response.getOutputStream());
    }

    //用户表导出
    @RequestMapping(value = "UserExcel")
    public void UserExport(HttpServletResponse response) throws IOException, ParseException {
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("信息表");
        List<UserMessage> userMessages = new ArrayList<UserMessage>();
        List<User> userList = userService.findAllUser();
        for(int i=0;i<userList.size();i++){
            UserMessage userMessage = new UserMessage();

            userMessage.setId(userList.get(i).getId());
            userMessage.setUserid(userList.get(i).getUserid());
            userMessage.setUsername(userList.get(i).getUsername());
            userMessage.setPassword(userList.get(i).getPassword());
            userMessage.setAddress(userList.get(i).getAddress());
            userMessage.setAdmin(userList.get(i).getAdmin());
            userMessage.setIslock(userList.get(i).getIslock());

            //今天是否打卡 1-打卡了 0-未打卡
            Integer flag = recordService.todaySubmit(userList.get(i).getId());
            userMessage.setIsdo(flag);

            //连续打卡天数
            Date date1 = new Date();
            Date date2 = new Date();
            List<String> str = recordService.getAllDateByUid(userList.get(i).getId());
            int num = 0;
            if(str.size() > 0) { //说明有打卡记录
                if (str.size() > 1) { //有两个以上的打卡记录
                    num = 1;
                    if(flag == 0){
                        Date date = new Date();
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                        String d = sdf.format(date); //今天的日期
                        date1 = sdf.parse(d);
                        date2 = sdf.parse(str.get(0));
                        int a = (int) ((date1.getTime() - date2.getTime()) / (1000 * 3600 * 24));
                        if (a != 1) num = 0; //说明昨天有打卡
                        //System.out.println("用户"+userList.get(i).getId()+"进入了判断");
                    }
                    if(num!=0) {
                        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                        //System.out.println("数组大小: " + str.size());
                        for (int j = 0; j < str.size() - 1; j++) {
                            date1 = format.parse(str.get(j));
                            //System.out.println("date1:" + date1);
                            date2 = format.parse(str.get(j + 1));
                            //System.out.println("date2:" + date2);
                            int a = (int) ((date1.getTime() - date2.getTime()) / (1000 * 3600 * 24));
                            //System.out.println("i=" + i + "，相差" + a + "天");
                            if (a == 1) num++;
                            else break;
                        }
                    }
                }//以下情况都是只有一个打卡记录
                else if (flag==1) num = 1; //说明只有今天打卡了
                else { //只有一个打卡记录，判断是不是昨天！
                    Date date = new Date();
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    String d = sdf.format(date); //今天的日期
                    date1 = sdf.parse(d);
                    date2 = sdf.parse(str.get(0));
                    int a = (int) ((date1.getTime() - date2.getTime()) / (1000 * 3600 * 24));
                    if (a == 1) num = 1; //说明昨天有打卡
                }
            }
            userMessage.setDodata(num);
            userMessages.add(userMessage);
        }
        // 设置要导出的文件的名字
        String fileName = "用户表"  + new Date() + ".xls";
        // 新增数据行，并且设置单元格数据
        int rowNum = 1;
        // headers表示excel表中第一行的表头 在excel表中添加表头
        String[] headers = { "id", "账号", "姓名","地址", "是否管理员","今日打卡","连续打卡天数","登录权限"};
        HSSFRow row = sheet.createRow(0);
        for(int i=0;i<headers.length;i++){
            HSSFCell cell = row.createCell(i);
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            cell.setCellValue(text);
        }
        //在表中存放查询到的数据放入对应的列
        for (UserMessage item : userMessages) {
            HSSFRow row1 = sheet.createRow(rowNum);
            row1.createCell(0).setCellValue(item.getId());
            row1.createCell(1).setCellValue(item.getUserid());
            row1.createCell(2).setCellValue(item.getUsername());
            row1.createCell(3).setCellValue(item.getAddress());
            if(item.getAdmin()==1){
                row1.createCell(4).setCellValue("是");
            }
            else {
                row1.createCell(4).setCellValue("否");
            }
            if(item.getIsdo()==1){
                row1.createCell(5).setCellValue("已打卡");
            }
            else {
                row1.createCell(5).setCellValue("未打卡");
            }
            row1.createCell(6).setCellValue(item.getDodata());
            if(item.getIslock()==0){
                row1.createCell(7).setCellValue("有");
            }
            else {
                row1.createCell(7).setCellValue("无");
            }
            rowNum++;
        }
        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        response.flushBuffer();
        workbook.write(response.getOutputStream());
    }

    //一个用户的上传记录表导出
    @RequestMapping(value = "OneRecordExcel")
    public void OneRecordExcel(HttpServletResponse response,@RequestParam("uid") Integer uid) throws IOException {
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("信息表");
        List<Record> records = recordService.findRecordByOneUid(uid);
        // 设置要导出的文件的名字
        String fileName = "上传记录表"  + new Date() + ".xls";
        // 新增数据行，并且设置单元格数据
        int rowNum = 1;
        // headers表示excel表中第一行的表头 在excel表中添加表头
        String[] headers = {"id","用户编号","姓名", "手机号码","紧急联系人","紧急联系人手机号码","身体状况","是否隔离","是否在校","目前所在地","体温","填写日期"};
        HSSFRow row = sheet.createRow(0);
        for(int i=0;i<headers.length;i++){
            HSSFCell cell = row.createCell(i);
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            cell.setCellValue(text);
        }
        //在表中存放查询到的数据放入对应的列
        for (Record item : records) {
            HSSFRow row1 = sheet.createRow(rowNum);
            row1.createCell(0).setCellValue(item.getId());
            row1.createCell(1).setCellValue(item.getUid());
            row1.createCell(2).setCellValue(item.getName());
            row1.createCell(3).setCellValue(item.getPhone());
            row1.createCell(4).setCellValue(item.getNamefriend());
            row1.createCell(5).setCellValue(item.getPhonefriend());
            switch (item.getBody()){
                case 1: {
                    row1.createCell(6).setCellValue("良好");
                    break;
                }
                case 2: {
                    row1.createCell(6).setCellValue("发烧");
                    break;
                }
                case 3: {
                    row1.createCell(6).setCellValue("咳嗽");
                    break;
                }
                case 4: {
                    row1.createCell(6).setCellValue("头晕");
                    break;
                }
                case 5: {
                    row1.createCell(6).setCellValue("腹泻");
                    break;
                }
                case 6: {
                    row1.createCell(6).setCellValue("乏力");
                    break;
                }
                case 7: {
                    row1.createCell(6).setCellValue("同时包含两种以上异常");
                    break;
                }
            }
            switch (item.getHospital()){
                case 1: {
                    row1.createCell(7).setCellValue("隔离中");
                    break;
                }
                case 0: {
                    row1.createCell(7).setCellValue("未隔离");
                    break;
                }
            }
            switch (item.getSchool()){
                case 1: {
                    row1.createCell(8).setCellValue("不在校");
                    break;
                }
                case 0: {
                    row1.createCell(8).setCellValue("在校");
                    break;
                }
            }
            row1.createCell(9).setCellValue(item.getAddress());
            row1.createCell(10).setCellValue(item.getTemperature());
            row1.createCell(11).setCellValue(item.getDate());
            rowNum++;
        }
        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        response.flushBuffer();
        workbook.write(response.getOutputStream());
    }

    //所有记录导出
    @RequestMapping(value = "AllRecordExcel")
    public void AllRecordExcel(HttpServletResponse response) throws IOException {
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("信息表");
        List<Record> records = recordService.findRecordAll();
        // 设置要导出的文件的名字
        String fileName = "所有上传记录表"  + new Date() + ".xls";
        // 新增数据行，并且设置单元格数据
        int rowNum = 1;
        // headers表示excel表中第一行的表头 在excel表中添加表头
        String[] headers = {"id","用户编号","姓名", "手机号码","紧急联系人","紧急联系人手机号码","身体状况","是否隔离","是否在校","目前所在地","体温","填写日期"};
        HSSFRow row = sheet.createRow(0);
        for(int i=0;i<headers.length;i++){
            HSSFCell cell = row.createCell(i);
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            cell.setCellValue(text);
        }
        //在表中存放查询到的数据放入对应的列
        for (Record item : records) {
            HSSFRow row1 = sheet.createRow(rowNum);
            row1.createCell(0).setCellValue(item.getId());
            row1.createCell(1).setCellValue(item.getUid());
            row1.createCell(2).setCellValue(item.getName());
            row1.createCell(3).setCellValue(item.getPhone());
            row1.createCell(4).setCellValue(item.getNamefriend());
            row1.createCell(5).setCellValue(item.getPhonefriend());
            switch (item.getBody()){
                case 1: {
                    row1.createCell(6).setCellValue("良好");
                    break;
                }
                case 2: {
                    row1.createCell(6).setCellValue("发烧");
                    break;
                }
                case 3: {
                    row1.createCell(6).setCellValue("咳嗽");
                    break;
                }
                case 4: {
                    row1.createCell(6).setCellValue("头晕");
                    break;
                }
                case 5: {
                    row1.createCell(6).setCellValue("腹泻");
                    break;
                }
                case 6: {
                    row1.createCell(6).setCellValue("乏力");
                    break;
                }
                case 7: {
                    row1.createCell(6).setCellValue("同时包含两种以上异常");
                    break;
                }
            }
            switch (item.getHospital()){
                case 1: {
                    row1.createCell(7).setCellValue("隔离中");
                    break;
                }
                case 0: {
                    row1.createCell(7).setCellValue("未隔离");
                    break;
                }
            }
            switch (item.getSchool()){
                case 1: {
                    row1.createCell(8).setCellValue("不在校");
                    break;
                }
                case 0: {
                    row1.createCell(8).setCellValue("在校");
                    break;
                }
            }
            row1.createCell(9).setCellValue(item.getAddress());
            row1.createCell(10).setCellValue(item.getTemperature());
            row1.createCell(11).setCellValue(item.getDate());
            rowNum++;
        }
        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        response.flushBuffer();
        workbook.write(response.getOutputStream());
    }

    //今日未打卡用户导出
    @RequestMapping(value = "NotDoUserExcel")
    public void NotDoUserExport(HttpServletResponse response) throws IOException, ParseException {
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("信息表");
        List<UserMessage> userMessages = new ArrayList<UserMessage>();
        List<User> userList = userService.findAllUser();
        for(int i=0;i<userList.size();i++){
            UserMessage userMessage = new UserMessage();
            userMessage.setId(userList.get(i).getId());
            userMessage.setUserid(userList.get(i).getUserid());
            userMessage.setUsername(userList.get(i).getUsername());
            userMessage.setPassword(userList.get(i).getPassword());
            userMessage.setAddress(userList.get(i).getAddress());
            userMessage.setAdmin(userList.get(i).getAdmin());
            userMessage.setIslock(userList.get(i).getIslock());
            //今天是否打卡 1-打卡了 0-未打卡
            Integer flag = recordService.todaySubmit(userList.get(i).getId());
            userMessage.setIsdo(flag);
            //连续打卡天数
            Date date1 = new Date();
            Date date2 = new Date();
            List<String> str = recordService.getAllDateByUid(userList.get(i).getId());
            int num = 0;
            if(str.size() > 0) { //说明有打卡记录
                if (str.size() > 1) { //有两个以上的打卡记录
                    num = 1;
                    if(flag == 0){
                        Date date = new Date();
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                        String d = sdf.format(date); //今天的日期
                        date1 = sdf.parse(d);
                        date2 = sdf.parse(str.get(0));
                        int a = (int) ((date1.getTime() - date2.getTime()) / (1000 * 3600 * 24));
                        if (a != 1) num = 0; //说明昨天有打卡
                        //System.out.println("用户"+userList.get(i).getId()+"进入了判断");
                    }
                    if(num!=0) {
                        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                        //System.out.println("数组大小: " + str.size());
                        for (int j = 0; j < str.size() - 1; j++) {
                            date1 = format.parse(str.get(j));
                            //System.out.println("date1:" + date1);
                            date2 = format.parse(str.get(j + 1));
                            //System.out.println("date2:" + date2);
                            int a = (int) ((date1.getTime() - date2.getTime()) / (1000 * 3600 * 24));
                            //System.out.println("i=" + i + "，相差" + a + "天");
                            if (a == 1) num++;
                            else break;
                        }
                    }
                }
                else if (flag==1) num = 1; //说明只有今天打卡了
                else { //只有一个打卡记录，判断是不是昨天！
                    Date date = new Date();
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    String d = sdf.format(date); //今天的日期
                    date1 = sdf.parse(d);
                    date2 = sdf.parse(str.get(0));
                    int a = (int) ((date1.getTime() - date2.getTime()) / (1000 * 3600 * 24));
                    if (a == 1) num = 1; //说明昨天有打卡
                }
            }
            userMessage.setDodata(num);
            if(flag==0){
                userMessages.add(userMessage);
            }
        }
        // 设置要导出的文件的名字
        String fileName = "未打卡用户表"  + new Date() + ".xls";
        // 新增数据行，并且设置单元格数据
        int rowNum = 1;
        // headers表示excel表中第一行的表头 在excel表中添加表头
        String[] headers = { "id", "账号", "姓名","地址", "是否管理员","今日打卡","连续打卡天数","登录权限"};
        HSSFRow row = sheet.createRow(0);
        for(int i=0;i<headers.length;i++){
            HSSFCell cell = row.createCell(i);
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            cell.setCellValue(text);
        }
        //在表中存放查询到的数据放入对应的列
        for (UserMessage item : userMessages) {
            HSSFRow row1 = sheet.createRow(rowNum);
            row1.createCell(0).setCellValue(item.getId());
            row1.createCell(1).setCellValue(item.getUserid());
            row1.createCell(2).setCellValue(item.getUsername());
            row1.createCell(3).setCellValue(item.getAddress());
            if(item.getAdmin()==1){
                row1.createCell(4).setCellValue("是");
            }
            else {
                row1.createCell(4).setCellValue("否");
            }
            if(item.getIsdo()==1){
                row1.createCell(5).setCellValue("已打卡");
            }
            else {
                row1.createCell(5).setCellValue("未打卡");
            }
            row1.createCell(6).setCellValue(item.getDodata());
            if(item.getIslock()==0){
                row1.createCell(7).setCellValue("有");
            }
            else {
                row1.createCell(7).setCellValue("无");
            }
            rowNum++;
        }
        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        response.flushBuffer();
        workbook.write(response.getOutputStream());
    }

    //体温异常导出
    @RequestMapping(value = "/overTemperatureExcel")
    public void overTemperatureExcel(HttpServletResponse response) throws IOException {
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("信息表");
        List<Record> records = recordService.overTemperature();
        // 设置要导出的文件的名字
        String fileName = "体温异常记录表"  + new Date() + ".xls";
        // 新增数据行，并且设置单元格数据
        int rowNum = 1;
        // headers表示excel表中第一行的表头 在excel表中添加表头
        String[] headers = {"id","用户编号","姓名", "手机号码","紧急联系人","紧急联系人手机号码","身体状况","是否隔离","是否在校","目前所在地","体温","填写日期"};
        HSSFRow row = sheet.createRow(0);
        for(int i=0;i<headers.length;i++){
            HSSFCell cell = row.createCell(i);
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            cell.setCellValue(text);
        }
        //在表中存放查询到的数据放入对应的列
        for (Record item : records) {
            HSSFRow row1 = sheet.createRow(rowNum);
            row1.createCell(0).setCellValue(item.getId());
            row1.createCell(1).setCellValue(item.getUid());
            row1.createCell(2).setCellValue(item.getName());
            row1.createCell(3).setCellValue(item.getPhone());
            row1.createCell(4).setCellValue(item.getNamefriend());
            row1.createCell(5).setCellValue(item.getPhonefriend());
            switch (item.getBody()){
                case 1: {
                    row1.createCell(6).setCellValue("良好");
                    break;
                }
                case 2: {
                    row1.createCell(6).setCellValue("发烧");
                    break;
                }
                case 3: {
                    row1.createCell(6).setCellValue("咳嗽");
                    break;
                }
                case 4: {
                    row1.createCell(6).setCellValue("头晕");
                    break;
                }
                case 5: {
                    row1.createCell(6).setCellValue("腹泻");
                    break;
                }
                case 6: {
                    row1.createCell(6).setCellValue("乏力");
                    break;
                }
                case 7: {
                    row1.createCell(6).setCellValue("同时包含两种以上异常");
                    break;
                }
            }
            switch (item.getHospital()){
                case 1: {
                    row1.createCell(7).setCellValue("隔离中");
                    break;
                }
                case 0: {
                    row1.createCell(7).setCellValue("未隔离");
                    break;
                }
            }
            switch (item.getSchool()){
                case 1: {
                    row1.createCell(8).setCellValue("不在校");
                    break;
                }
                case 0: {
                    row1.createCell(8).setCellValue("在校");
                    break;
                }
            }
            row1.createCell(9).setCellValue(item.getAddress());
            row1.createCell(10).setCellValue(item.getTemperature());
            row1.createCell(11).setCellValue(item.getDate());
            rowNum++;
        }
        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        response.flushBuffer();
        workbook.write(response.getOutputStream());
    }

    //处在风险地区导出
    @RequestMapping(value = "/areaExcel")
    public void areaExcel(HttpServletResponse response) throws IOException {
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("信息表");
        List<Record> records = recordService.areaRecord();
        // 设置要导出的文件的名字
        String fileName = "处在风险地区表"  + new Date() + ".xls";
        // 新增数据行，并且设置单元格数据
        int rowNum = 1;
        // headers表示excel表中第一行的表头 在excel表中添加表头
        String[] headers = {"id","用户编号","姓名", "手机号码","紧急联系人","紧急联系人手机号码","身体状况","是否隔离","是否在校","目前所在地","体温","填写日期"};
        HSSFRow row = sheet.createRow(0);
        for(int i=0;i<headers.length;i++){
            HSSFCell cell = row.createCell(i);
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            cell.setCellValue(text);
        }
        //在表中存放查询到的数据放入对应的列
        for (Record item : records) {
            HSSFRow row1 = sheet.createRow(rowNum);
            row1.createCell(0).setCellValue(item.getId());
            row1.createCell(1).setCellValue(item.getUid());
            row1.createCell(2).setCellValue(item.getName());
            row1.createCell(3).setCellValue(item.getPhone());
            row1.createCell(4).setCellValue(item.getNamefriend());
            row1.createCell(5).setCellValue(item.getPhonefriend());
            switch (item.getBody()){
                case 1: {
                    row1.createCell(6).setCellValue("良好");
                    break;
                }
                case 2: {
                    row1.createCell(6).setCellValue("发烧");
                    break;
                }
                case 3: {
                    row1.createCell(6).setCellValue("咳嗽");
                    break;
                }
                case 4: {
                    row1.createCell(6).setCellValue("头晕");
                    break;
                }
                case 5: {
                    row1.createCell(6).setCellValue("腹泻");
                    break;
                }
                case 6: {
                    row1.createCell(6).setCellValue("乏力");
                    break;
                }
                case 7: {
                    row1.createCell(6).setCellValue("同时包含两种以上异常");
                    break;
                }
            }
            switch (item.getHospital()){
                case 1: {
                    row1.createCell(7).setCellValue("隔离中");
                    break;
                }
                case 0: {
                    row1.createCell(7).setCellValue("未隔离");
                    break;
                }
            }
            switch (item.getSchool()){
                case 1: {
                    row1.createCell(8).setCellValue("不在校");
                    break;
                }
                case 0: {
                    row1.createCell(8).setCellValue("在校");
                    break;
                }
            }
            row1.createCell(9).setCellValue(item.getAddress());
            row1.createCell(10).setCellValue(item.getTemperature());
            row1.createCell(11).setCellValue(item.getDate());
            rowNum++;
        }
        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        response.flushBuffer();
        workbook.write(response.getOutputStream());
    }

}
